<script language="javascript" type="text/javascript">
    $(window).scroll(
    function()
    {
        scrollTop = $(window).scrollTop();
        docHeight = getDocHeight();
        winHeight = getWinHeight();
        scrolltop20 = Math.round((getDocHeight() - getWinHeight())*0.9);
        result = getDocHeight() - getWinHeight();
		
        console.log('Top Scroll: '+ scrollTop +' || Win Height: '+ winHeight +' || Doc Height: '+ docHeight+' || scrolltop20: '+scrolltop20+' || result :'+ result );
	
        if($(window).scrollTop() >= scrolltop20)
        {
            if(killScroll == false) 
            {
                killScroll = true;
				
                $('#loading').show();
                $('#loadding-text').show();			
                $('#loading').html('<img src="images/loading.gif" alt="" width="150" height="13" />');
					
                if($("li:last").data('ctype') == 'marketSearch')
                {
                    marketSearch('marketSearch', $("li:last").data('wordsearch'), $("li:last").data('productstatus'), $("li:last").data('categoryid'), $("li:last").data('tradeid'), $("li:last").data('provinceid')); 
                }					
                setTimeout(function(){killScroll = false},500);	
            }
        }
    }
); 
</script>
<!-- Start of Search Market page -->
<div data-role="page" id="searchMarket">
    <div data-role="header">
        <h1>ผลการค้นหา</h1>
        <a rel="external" id="link_web" href="?q=market" data-icon="back" data-iconpos="right" data-transition="slide">กลับ</a>        
    </div><!-- /header -->

    <div data-role="content">	
        <?php
        /*
          Array ( [product_search] => sea [product_status] => 0 [category] => 0 [trade] => 0 [province] => 0 )
          id 	name 	product_status 	category_id 	trade_id 	province_id  -- products

         * ***********************************
          Array ( [q] => marketSearch [product_search] => แ [product_status] => 0 [category] => 0 [trade] => 0 [province] => 0 )


          SELECT id, name, product_status, category_id, trade_id, province_id FROM products WHERE name like "%แ%" ORDER BY id DESC LIMIT 20
         * ***********************************
         */
        ?>
        <ul data-role="listview">
            <?php
            $sql = 'SELECT * FROM products WHERE name like "%' . $db->antihack($_GET['product_search']) . '%" ';

            if($_GET['product_status'] != 0)
            {
                $sql .='AND product_status = ' . $db->antihack($_GET['product_status']) . ' ';
            }

            if($_GET['category'] != 0)
            {
                $sql .='AND category_id = ' . $db->antihack($_GET['category']) . ' ';
            }

            if($_GET['trade'] != 0)
            {
                $sql .='AND trade_id = ' . $db->antihack($_GET['trade']) . ' ';
            }

            if($_GET['province'] != 0)
            {
                $sql .='AND province_id = ' . $db->antihack($_GET['province']) . ' ';
            }

            $sql .= 'ORDER BY id DESC LIMIT 20 ';
            $res = $db->select_query($sql);
            $numRow = $db->rows($res);
            if($numRow > 0)
            {
                while($arr = $db->fetch($res))
                {
                    $countReply = $db->num_rows('product_reply', 'reply_id', 'product_id = ' . $arr['id'] . '');
                    ?>
                    <li data-ctype="marketSearch" data-wordsearch='<?php echo $_GET['product_search']; ?>' id="market_<?php echo $arr['id']; ?>" last_id="<?php echo $arr['id']; ?>" data-productstatus='<?php echo $_GET['product_status']; ?>' data-categoryid='<?php echo $_GET['category']; ?>' data-tradeid='<?php echo $_GET['trade']; ?>' data-provinceid='<?php echo $_GET['province']; ?>'>
                        <a rel="external" href="?q=productDetail&pid=<?php echo $arr['id']; ?>">
                            <img src="<?php echo $arr['tn_part']; ?>" alt="" width="80" height="80" />
                            <h3><?php echo $arr['name']; ?></h3>
                            <p><?php echo $arr['headline']; ?></p>
                            <span class="ui-li-count ui-btn-up-c ui-btn-corner-all"><?php echo $countReply; ?></span>
                        </a> 
                    </li>
                    <?php
                }
            }
            else
            {
                ?>
                <li>
                    <h3>--  ไม่พบข้อมูลในระบบ --</h3>
                </li>
                <?php
            }
            ?>
        </ul>

        <?php
        $numReply = $db->rows($res);
        if($numReply != 0)
        {
            ?>
            <div id="loadding-text" style="clear:both;" align="center">&nbsp;</div>  
            <div id="loading" align="center"></div>
            <?php
        }
        ?> 
    </div><!-- /content -->

    <div data-role="footer" >
        <h4>Version 1.0 Beta</h4>
        <a id="link_web" href="http://www.mocyc.com" data-icon="refresh" data-iconpos="right" class="ui-btn-right">แบบปกติ</a>

    </div><!-- /footer -->
</div><!-- /page -->